package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import sparrowGui.skin.PageSkin;
	import sparrowGui.utils.SparrowUtil;
	
	/** 页码改变. **/
	[Event(name="page_change", type="sparrowGui.PageBox")]
	/** 页码改变. **/
	[Event(name="max_page_change", type="sparrowGui.PageBox")]
	/**
	 * 翻页组件,和列表组合使用
	 * @author Pelephone
	 */
	public class PageBox extends BaseUIModule
	{
		/**
		 * 页码改变
		 */
		public static const PAGE_CHANGE:String = "page_change";
		/**
		 * 最大页数改变
		 */
		public static const MAX_PAGE_CHANGE:String = "max_page_change";
		
		private var _currentPage:int;
		private var _maxPage:int;
		
		private var _langShow:String = "{currentPage}/{maxPage}";
		
		public function PageBox(pageSkin:DisplayObject=null)
		{
			super(pageSkin);
		}
		
		override public function create(argSkin:DisplayObject):void
		{
			argSkin = argSkin || new PageSkin;
		}
		
		override public function addListener():void
		{
			skin.addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		override public function removeListener():void
		{
			skin.removeEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		/**
		 * 点击事件
		 * @param e
		 */
		private function onMouseClick(e:MouseEvent):void
		{
			switch(e.target.name)
			{
				case BTN_JUMPPAGE_NAME:
				{
					// 跳转必须有页数文本，不然会报错
					currentPage = int(txt_inputNum.text);
					break;
				}
				case BTN_PREV_NAME:
				{
					if(currentPage>0)
						currentPage--;
					break;
				}
				case BTN_NEXT_NAME:
				{
					if(currentPage<maxPage)
						currentPage++;
					break;
				}
				case BTN_LAST_NAME:
				{
					currentPage = maxPage;
					break;
				}
				case BTN_FIRST_NAME:
				{
					currentPage = 0;
					break;
				}
			}
		}
		
		/**
		 * 更新页码显示
		 */
		private function flushPage():void
		{
			if(txt_inputNum)
			{
				txt_inputNum.text = String(currentPage);
			}
			if(txt_showNum)
			{
				var str:String = SparrowUtil.langByObj(langShow,this);
				txt_showNum.text = str;
			}
		}

		/**
		 * 当前页
		 */
		public function get currentPage():int
		{
			return _currentPage;
		}

		/**
		 * @private
		 */
		public function set currentPage(value:int):void
		{
			_currentPage = value;
			flushPage();
			sendNote(PAGE_CHANGE);
		}

		/**
		 * 最大页数
		 */
		public function get maxPage():int
		{
			return _maxPage;
		}

		/**
		 * @private
		 */
		public function set maxPage(value:int):void
		{
			_maxPage = value;
			flushPage();
		}

		/**
		 * 显示文字的语言包
		 */
		public function get langShow():String
		{
			return _langShow;
		}

		/**
		 * @private
		 */
		public function set langShow(value:String):void
		{
			_langShow = value;
			flushPage();
		}

		
		// 组件里面的按钮名字 //////
		
		private static const BTN_JUMPPAGE_NAME:String	= "btn_jumppage";
		private static const BTN_PREV_NAME:String		= "btn_prev";
		private static const BTN_NEXT_NAME:String		= "btn_next";
		private static const BTN_LAST_NAME:String		= "btn_last";
		private static const BTN_FIRST_NAME:String	= "btn_first";
		private static const TXT_INPUTNUM_NAME:String	= "txt_inputNum";
		private static const TXT_SHOWNUM_NAME:String	= "txt_showNum";
//		private static const TXT_PERNUM:String	= "txt_perNum";
//		private static const COMBOBOX_PAGE:String	= "combobox_Page";
		
		// 组件里面的按钮引用
		public function get btn_jumppage():DisplayObject 
		{ return skin[BTN_JUMPPAGE_NAME]; }
		public function get btn_prev():DisplayObject 
		{ return skin[BTN_PREV_NAME]; }
		public function get btn_next():DisplayObject 
		{ return skin[BTN_NEXT_NAME]; }
		public function get btn_last():DisplayObject 
		{ return skin[BTN_LAST_NAME]; }
		public function get btn_first():DisplayObject 
		{ return skin[BTN_FIRST_NAME]; }
		public function get txt_inputNum():TextField 
		{ return skin[TXT_INPUTNUM_NAME]; }
		public function get txt_showNum():TextField 
		{ return skin[TXT_SHOWNUM_NAME]; }
	}
}